package com.example.haiti.infrastructure.surveyor;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.SQLException;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

public class questionnaire_Activity1 extends Activity {
	
    private Utils utilsHelper = null;
    private Cursor structureCursor = null;
    private int structureID;
	public Context mContext;
	
	private EditText Edit_surveyorName = null;    
    private EditText Edit_surveyorPhoneNumber= null;
    private EditText Edit_structureAddress= null;
    


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.questionnaire_layout_1);         	
        
        utilsHelper = new Utils(questionnaire_Activity1.this);      

        utilsHelper.openDatabase(); 

    	getStructure();       
        manageEditTextFields();
        setupNextButton();   
    } // end of the onCreate method
    
    @Override
    protected void onPause() {
        super.onPause();

        utilsHelper.closeDatabase();
    }
    
    @Override
    protected void onStop() {
      super.onStop();
       
      utilsHelper.closeDatabase();
    }
    
        
   private void getStructure(){
	   // Get structure ID
	   structureID = getIntent().getIntExtra("structureID", 1000); //Get structureID otherwise 1000      
	   structureCursor = utilsHelper.getStructureCursor(structureID);	  	   
   }
    
    private void manageEditTextFields(){
	    	
		// Declares the text input fields
	    Edit_surveyorName = (EditText) this.findViewById(R.id.editFullName);     
	    Edit_surveyorPhoneNumber = (EditText) this.findViewById(R.id.editPhoneNumber); 
	    Edit_structureAddress = (EditText) this.findViewById(R.id.editAddress);     
	    
	    // Checks if data already exists
	    String oldFullName = structureCursor.getString(structureCursor.getColumnIndex(SqlHelper.COLUMN_SURVEYOR_NAME));
		String oldPhoneNumber = structureCursor.getString(structureCursor.getColumnIndex(SqlHelper.COLUMN_SURVEYOR_PHONE_NUMBER));
	    String oldAddress = structureCursor.getString(structureCursor.getColumnIndex(SqlHelper.COLUMN_STRUCTURE_ADDRESS));
	
	    
		// Sets the text of the input fields
		Edit_surveyorName.setText(oldFullName);
		Edit_surveyorPhoneNumber.setText(oldPhoneNumber);
		Edit_structureAddress.setText(oldAddress);	     	
    }   
   
    private void setupNextButton(){
    	 	
		// Add Next button
	    Button btnNextQuestions = (Button) findViewById(R.id.buttonNext);
	    btnNextQuestions.setOnClickListener(new OnClickListener() {
	        	        
	    	public void onClick(View v) {
	                    	         
	     // Defines strings input in by the user
	        String surveyorName = Edit_surveyorName.getText().toString();
	        String surveyorPhoneNumber = Edit_surveyorPhoneNumber.getText().toString();
	        String structureAddress = Edit_structureAddress.getText().toString();
	                       
	        String[][] databaseUpdates = {
	            {SqlHelper.COLUMN_SURVEYOR_NAME, surveyorName},
	            {SqlHelper.COLUMN_STRUCTURE_ADDRESS, structureAddress},
	            {SqlHelper.COLUMN_SURVEYOR_PHONE_NUMBER, surveyorPhoneNumber},
	            {SqlHelper.COLUMN_STRUCTURE_STATUS, "In Progress"} 
	
	            };               
	        utilsHelper.dbHelper.updateStructureEntry(databaseUpdates, structureID ); 
	        
	        utilsHelper.startNextActivity(structureID, questionnaire_Activity2.class);      
	        }
	    }); // End of  Next button click listener      
    	
    }
    // Back Button Pressed
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        if ((keyCode == KeyEvent.KEYCODE_BACK))
        {
        	utilsHelper.startNextActivity(structureID, main_Activity.class);        
        }
        return super.onKeyDown(keyCode, event);
    }
} 